﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Lucene.Net.Search;
using PRDP.Search;
using PRDP.Tools;
using PRDP.DS.Search;

public partial class PRDPSearcher : System.Web.UI.Page
{
    //来自页面的查询请求参数
    public QueryParameter queryParameter { get; set; }
    //检索位置
    string position;

    protected void Page_Load(object sender, EventArgs e)
    {
        //初始化
        this.queryParameter = new QueryParameter();
        //获得相关参数
        if (Request["rank"] != null)
            this.queryParameter.Rank = Request["rank"];
        if (Request["page"] != null)
            this.queryParameter.Page = Convert.ToInt32(Request["page"]);
        if (Request["isSelf"] != null)
            this.queryParameter.IsSelf = (Request["isSelf"] == "false") ? false : true;
        if (Request["language"] != null)
            this.queryParameter.Language = Request["language"];
        if (Request["position"] != null)
            this.position = Request["position"];

        //判断是否为跨页面提交
        if (PreviousPage != null)
        {
            if (Request.Form["from"] == null || Request.Form["from"] == "")
            {
                Response.Redirect("admin/msg.aspx?msg=参数错误，请返回！&gourl=../Default.aspx", true);
            }
            else
            {
                //判断来源
                this.queryParameter.From = Request.Form["from"];
                if (this.queryParameter.From == "advance")
                {
                    if (Request.QueryString["searchword"] == null || Request.QueryString["searchword"] == "")
                    {
                        this.queryParameter.SearchTypeID = Int32.Parse(Request.Form["ctl00$content$ddlSearchType"]);
                        this.queryParameter.Language = Request.Form["ctl00$content$ddlLanguage"];
                        for (int i = 0; i < Request.Form.Count; i++)
                        {
                            if (Request.Form.Keys[i].ToString().StartsWith("position_"))
                            {
                                string tempPosition = Request.Form[i].ToString();
                                string tempKeyword = Request.Form["keyword_" + tempPosition];
                                if (tempKeyword != "")
                                {
                                    string tempLogic = Request.Form["logic_" + tempPosition];
                                    QueryWord word = new QueryWord(tempPosition, tempKeyword, tempLogic);
                                    this.queryParameter.KeywordList.Add(word);
                                }
                            }
                        }
                        if (this.queryParameter.KeywordList.Count == 0)
                            Response.Redirect("admin/msg.aspx?msg=请输入关键词！&gourl=../advancesearch.aspx", true);
                    }
                    else
                    {
                        this.queryParameter.SearchTypeID = Int32.Parse(Request["searchType"]);
                        QueryWord word = new QueryWord(Request["searchword"],this.position);
                        this.queryParameter.KeywordList.Add(word);
                    }
                }
                else if (this.queryParameter.From == "index")
                {
                    TextBox searchBox = (TextBox)(PreviousPage.FindControl("searchId"));
                    HiddenField searchTypeField = (HiddenField)(PreviousPage.FindControl("searchType"));
                    this.queryParameter.SearchTypeID = Int32.Parse(searchTypeField.Value);
                    if (searchBox.Text != "")
                    {
                        QueryWord word = new QueryWord(searchBox.Text, this.position);
                        this.queryParameter.KeywordList.Add(word);
                    }
                }
            }

        }
        else
        {
            if (Request["searchType"] != null && Request["searchType"] != "")
            {
                this.queryParameter.SearchTypeID = Int32.Parse(Request["searchType"]);
            }
            if (Request["keyword"] != null && Request["keyword"] != "")
            {
                this.queryParameter.KeywordList.Add(new QueryWord(Request["keyword"]));
            }
        }

        //输出客户端JS

        Page.ClientScript.RegisterStartupScript(this.GetType(), "searchType","queryExpression = '" + this.queryParameter.getQueryExpression() + "';"
            + "queryParameterJSON = '" + new JavaScriptSerializer().Serialize(this.queryParameter) + "';"
            + "ajaxSearch();"
            , true);
    }

}
